package com.example.save;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

public class fragment7 extends Fragment {
    private Button button1;
    private Button button2;

    public fragment7(){
        super(R.layout.fragment5);
    }

    @Override
    public void onViewCreated(View view,Bundle savedStanceState) {
        super.onViewCreated(view, savedStanceState);

        final EditText usernameEdit = getView().findViewById(R.id.getNameEdit);
        final EditText ageEdit = getView().findViewById(R.id.getAgeEdit2);
        final TextView contentTextView = getView().findViewById(R.id.Text);
        DbHelper dbHelper = new DbHelper(getActivity());

        button1 = getActivity().findViewById(R.id.button5);
        button1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {



                String nameEdit=usernameEdit.getText().toString();
                String ageE=ageEdit.getText().toString();
                String msg=null;
                //添加数据
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username", nameEdit);
                values.put("age", ageE);

                long ret = db.insert("users", null, values);


                if(nameEdit==""||ageE=="0"||ret==-1){
                    msg ="添加失败" ;
                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                }
                else {
                    msg ="添加成功" ;
                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                }
                db.close();

                db = dbHelper.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();

                Cursor cursor = db.query("users", null, null, null, null,
                        null, null);
                while (cursor.moveToNext()) {
                    long id = cursor.getLong(cursor.getColumnIndex("id"));
                    String username = cursor.getString(cursor.getColumnIndex("username"));
                    int age = cursor.getInt(cursor.getColumnIndex("age"));
                    stringBuilder.append("id:").append(id+" ")
                            .append("username:").append(username+" ")
                            .append("age:").append(age)
                            .append("\n");
                }
                contentTextView.setText(stringBuilder.toString());
                db.close();

            }
        });

        button2=getActivity().findViewById(R.id.button6);
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                String selection="age<18";
                Cursor cursor = db.query("users", null, selection, null, null,
                        null, null);
                while (cursor.moveToNext()) {
                    long id = cursor.getLong(cursor.getColumnIndex("id"));
                    String username = cursor.getString(cursor.getColumnIndex("username"));
                    int age = cursor.getInt(cursor.getColumnIndex("age"));
                    stringBuilder.append("id:").append(id+" ")
                            .append("username:").append(username+" ")
                            .append("age:").append(age)
                            .append("\n");
                }
                contentTextView.setText(stringBuilder.toString());
                db.close();
            }
        });

    }


    }


